package io.github.doc.ui;

import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.FormBuilder;
import io.github.doc.config.PluginSetting;
import io.github.doc.config.state.PluginSettingsState;

/**
 * <p>Company: 深圳振瀚信息技术有限公司成都分公司 </p>
 * <p>Description: </p>
 *
 * @author ZH.Team
 * @version 1.0.0
 * @email "mailto:ZH.Team@zhxx.com"
 * @date 2024.12.01 20:26
 * @since y.y.y
 */
public class TextComponentWrapper extends AbstractComponentWrapper<String, JBTextField> {

    /**
     * J component wrapper
     *
     * @param title        title
     * @param key          key
     * @param defaultValue default value
     * @since y.y.y
     */
    public TextComponentWrapper(String title, String key, String defaultValue) {
        super(title, new JBTextField(), key);
        this.setDefaultValue(defaultValue);
    }

    /**
     * Builder component
     *
     * @param formBuilder form builder
     * @since y.y.y
     */
    @Override
    public void builderComponent(FormBuilder formBuilder) {
        formBuilder.addLabeledComponent(this.getTitle(), this.getComponent());
    }

    /**
     * Load
     *
     * @param pluginSetting plugin setting
     * @since y.y.y
     */
    @Override
    public void load(PluginSetting pluginSetting) {
        String value = pluginSetting.getConfig(this.getKey());
        this.getComponent().setText(value != null ? value : this.getDefaultValue());
    }

    /**
     * Gets value *
     *
     * @return the value
     * @since y.y.y
     */
    @Override
    public String getValue() {
        return this.getComponent().getText();
    }

    /**
     * Save
     *
     * @since y.y.y
     */
    @Override
    public void save() {
        String value = this.getValue();
        this.doSave(value);
    }
}
